<?php 
	session_start();
	include("header.php");
	include("top_menu.php");
	include("instr_panel.php");
?>
  <script type="text/javascript"
          src="http://maps.googleapis.com/maps/api/js?key=AIzaSyAM2jgOI4ve73j1XimvvPYiK-S47emFAiY&sensor=false&language=en">
  </script>

  <script type="text/javascript" 
          src="scripts/util.js">
  </script>

  <script type="text/javascript">
    var map;
    var geocoder;

    function load() {
      geocoder = new google.maps.Geocoder();
      var mapOptions = {
          center: new google.maps.LatLng(48.6833,67.7),
          zoom: 5,
          mapTypeId: google.maps.MapTypeId.ROADMAP
      };
      
      map = new google.maps.Map(document.getElementById("map"), mapOptions);
      
      //find layers to view
      var markerTypes = 0;
      if (document.getElementById('layer1').checked) {
        markerTypes |= Math.pow(2 , 1);
      }
      if (document.getElementById('layer2').checked) {
        markerTypes |= Math.pow(2 , 2);
      }
      
      //place markers on map
      downloadUrl("scripts/xml_generator.php", function(data) {          
        var markers = data.documentElement.getElementsByTagName("marker");
        for (var i = 0; i < markers.length; i++) {
          var address = markers[i].getAttribute("place");
          var point = new google.maps.LatLng(parseFloat(markers[i].getAttribute("lng")),
                                  parseFloat(markers[i].getAttribute("lat")));
          var type_id = markers[i].getAttribute("type_id");
          var type_id_int = parseInt(type_id);
          if ((markerTypes & Math.pow(2 , type_id_int)) == 0) continue;
          var info = markers[i].getAttribute("info");
          createMarker(point , address , type_id , info);
        }
      });
      
      //add listener to map for right clicks
      google.maps.event.addListener(map , 'rightclick' , function(event) {
        var lat = parseFloat(event.latLng.lat());
        var lng = parseFloat(event.latLng.lng());
        var latlng = new google.maps.LatLng(lat , lng);          
        geocoder.geocode({'latLng' : latlng} , function(results , status) {
          if (status == google.maps.GeocoderStatus.OK) {
            if (results[1]) {
              document.getElementById('place').value = results[1].formatted_address;
            }
          }
        });
      });
    }

    //function for creating markers on map
    function createMarker(point, address, type_id , info) {
      var marker;
      if (type_id == 1) {
   		  marker = new google.maps.Marker({
          position: point,
          title: address,
          animation: google.maps.Animation.DROP,
          icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png'
        });
      } else if (type_id == 2) {
        marker = new google.maps.Marker({
          position: point,
          title: address,
          animation: google.maps.Animation.DROP,
          icon: 'http://labs.google.com/ridefinder/images/mm_20_blue.png'
        });
      }
      marker.setMap(map);
      var infowindow = new google.maps.InfoWindow({
        content: info
      });
      google.maps.event.addListener(marker , 'click' , function() {
        infowindow.open(map , marker)
      });
    }

  </script>
 
 <title> Main page </title>
<?php
	if (isset($_SESSION['username'])) {
?>
  <body onload="load()">
    <div id="map"></div>
  </body>
<?php } ?>